package com.project.filter;

import com.project.bean.UserBean;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class AddFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse  response = (HttpServletResponse) servletResponse;

        //从session中取出登陆用户
        UserBean user = (UserBean) request.getSession()
                .getAttribute("loginUser");
        if(user == null){//没有登陆,重定向到登陆页面
            response.getWriter().print("notGrade");
        }
        else{
            //放行，让请求继承访问目标资源
            filterChain.doFilter(request,response);
        }
    }
}
